package com.xhh.nphm.web.controller.system;

import com.xhh.nphm.common.core.controller.BaseController;
import com.xhh.nphm.common.core.domain.Ztree;
import com.xhh.nphm.system.domain.BSort;
import com.xhh.nphm.system.mapper.SortMapper;
import com.xhh.nphm.system.service.impl.CommonServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/system/common")
public class SysCommonController extends BaseController {
    private String prefix ="system/common";
    @Autowired
    private CommonServiceImpl commonService;
    @Autowired
    private SortMapper sortMapper;

    /**
     * 查询内容类别
     * @param request HttpServletRequest
     * @return
     */
    @RequestMapping("/findContentCate")
    public Object findContentCate(HttpServletRequest request) {
        List<Map> list = commonService.findContentCate();
        return list.get(0).get("contentCate");
    }

    /**
     * 查询交办意见，坐席处理
     * @param request HttpServletRequest
     * @return
     */
    @RequestMapping("/opinion")
    public Object opinion(HttpServletRequest request) {
        List<Map> list = commonService.findOpinion();
        return list;
    }



    /**
     * 查询坐席备注
     * @param request HttpServletRequest
     * @return
     */
    @RequestMapping("/seatsRemark")
    public Object seatsRemark(HttpServletRequest request) {
        List<Map> list = commonService.findSeatsRemark();
        return list;
    }

    /**
     * 查询地区
     * @param request HttpServletRequest
     * @return
     */
    @RequestMapping("/area")
    public Object area(HttpServletRequest request) {
        List<Map> list = commonService.findArea();
        return list;
    }




    /**
     * 查询证件类型
     * @param request HttpServletRequest
     * @return
     */
    @RequestMapping("/cerType")
    public Object cerType(HttpServletRequest request) {
        List<Map> list = commonService.findCerType();
        return list;
    }

    /**
     * 查询电话类型
     * @param request HttpServletRequest
     * @return EasyuiPage
     */
    @RequestMapping("/telType")
    public Object telType(HttpServletRequest request) {
        List<Map> list = commonService.findTelType();
        return list;
    }

    /**
     * 查询工单类型
     * @return EasyuiPage
     */
    @RequestMapping("/workFormType")
    public List<Map> workFormType() {
        List<Map> list = commonService.findWorkFormType();
        return list;
    }

    /**
     * 查询办理方式
     * @return
     */
    @RequestMapping("/banliType")
    public List<Map> findBanliType() {
        return commonService.findBanliType();
    }


    /**
     * 查询内容分类树形
     * @param request HttpServletRequest
     * @return E
     * asyuiPage
     */
    @CrossOrigin
    @RequestMapping("/findContentCategories")
    public Object findContentCategories(HttpServletRequest request) {
        List<Map<String, Object>> trees = new ArrayList<Map<String, Object>>();
        Map<String, Object> params = new HashMap<>();
        List<Map> list = commonService.findContentCategories();
        for(Map<String, Object> map : list) {
            params.put("id", map.get("CodePK"));
            params.put("pId", map.get("CodeParent"));
            params.put("name", map.get("SortName"));
            params.put("title", map.get("SortName"));
            params.put("text", map.get("SortName"));
//            if("商品房、二手房".equals(map.get("SortName").toString())
//                    || map.get("CodePK").toString().equals("110710")
//            || map.get("CodePK").toString().equals("1107")) {
//                params.put("checked", true);
//                params.put("open", true);
//                params.put("nocheck", true);
//            }
//            else {

            params.put("checked", false);
            params.put("open", false);
            params.put("nocheck", false);
//            }
            trees.add(params);
            params = new HashMap<>();
        }
        return trees;
    }

    /**
     * 查询内容分类树形
     * @param request HttpServletRequest
     * @return E
     * asyuiPage
     */
    @GetMapping(value = {"/findContentCategories/{codepk}","/findContentCategories/{codepk}/{excludeId}"})
    public ModelAndView findContentCategories(@PathVariable("codepk") String codepk,
                                              @PathVariable(value = "excludeId", required = false) String excludeId, ModelMap mmap,
                                              HttpServletRequest request) {
        mmap.put("sort",sortMapper.selectByCodePk(codepk));
        mmap.put("excludeId", excludeId);
        return new ModelAndView("system/mTelRcd/tree");
    }

    /**
     * 加载部门列表树
     */
    @GetMapping("/treeData")
    @ResponseBody
    public List<Ztree> treeData() {
        List<Ztree> ztrees = commonService.selectSortList();
        return ztrees;
    }

    /**
     * 加载部门列表树（排除下级）
     */
    @GetMapping("/treeData/{excludeId}")
    @ResponseBody
    public List<Ztree> treeDataExcludeChild(@PathVariable(value = "excludeId", required = false) Long excludeId) {
        BSort bSort = new BSort();
        bSort.setCodepk(excludeId.toString());
        List<Ztree> ztrees = commonService.selectSortTreeExcludeChild(bSort);
        return ztrees;
    }

    /**
     * 选择承办部门 页面
     *
     * @param map
     * @return
     */
    @GetMapping("/dirDeptS")
    public ModelAndView dirDeptS(ModelMap map) {
        return new ModelAndView(prefix+"/dirDeptS");
    }


    /**
     * 查询本级部门
     * @param fpArea
     * @param deptNo
     * @return
     */
    @PostMapping("/flatDept")
    public List<Map> queryFlatDept(String fpArea, String deptNo) {
        return commonService.queryFlatDept(fpArea, deptNo);
    }

    /**
     * 查询下级部门
     * 区(市)县
     * @param fpParent
     * @return
     */
    @PostMapping("/sonDept")
    public List<Map> querySonDept(String fpParent) {
        return commonService.querySonDept(fpParent);
    }
}
